﻿using System;
using System.Data;
using System.Data.Common;
using TodoSoft.Data;

namespace Finance
{
	public class DataBase :IDisposable
	{
		private readonly DbProviderFactory _dbProviderFactory;
		public SqlCommandProcessorBase Sql { get; protected set; }

		public DataBase(DbProviderFactory dbProviderFactory)
		{
			_dbProviderFactory = dbProviderFactory;
		}

		public DbConnection Connection { get; protected set; }

		public virtual void OpenConnection(string connectionString)
		{
			Connection = _dbProviderFactory.CreateConnection();
			Connection.ConnectionString = connectionString;
			Connection.Open();
		}
    
		#region IDisposable Members

		public void Dispose()
		{
			if (Connection != null)
			{
				CloseConnection();
			}
		}

		#endregion

		public void CloseConnection()
		{
			Connection.Close();
			Connection.Dispose();
			Connection = null;
		}

	}
}
